home *** CD-ROM | disk | FTP | other *** search
/ Atari Mega Archive 2 / Atari Mega Archive CD - Volume 2.iso / minix / up1510b.tgz / up1510b / src / commands / tee.c < prev    next >
C/C++ Source or Header  |  1990-07-19  |  1KB  |  63 lines

  1. /* tee - pipe fitting            Author: Paul Polderman */
  2.  
  3. #include <sys/types.h>
  4. #include <fcntl.h>
  5. #include <blocksize.h>
  6. #include <signal.h>
  7. #include <unistd.h>
  8.  
  9. #define    MAXFD    18
  10.  
  11. int fd[MAXFD];
  12.  
  13. main(argc, argv)
  14. int argc;
  15. char **argv;
  16. {
  17.   char iflag = 0, aflag = 0;
  18.   char buf[BLOCK_SIZE];
  19.   int i, s, n;
  20.  
  21.   argv++;
  22.   --argc;
  23.   while (argc > 0 && argv[0][0] == '-') {
  24.     switch (argv[0][1]) {
  25.         case 'i':        /* Interrupt turned off. */
  26.         iflag++;
  27.         break;
  28.         case 'a':        /* Append to outputfile(s), instead of
  29.              * overwriting them. */
  30.         aflag++;
  31.         break;
  32.         default:
  33.         std_err("Usage: tee [-i] [-a] [files].\n");
  34.         exit(1);
  35.     }
  36.     argv++;
  37.     --argc;
  38.   }
  39.   fd[0] = 1;            /* Always output to stdout. */
  40.   for (s = 1; s < MAXFD && argc > 0; --argc, argv++, s++) {
  41.     if (aflag && (fd[s] = open(*argv, O_RDWR)) >= 0) {
  42.         lseek(fd[s], 0L, SEEK_END);
  43.         continue;
  44.     } else {
  45.         if ((fd[s] = creat(*argv, 0666)) >= 0) continue;
  46.     }
  47.     std_err("Cannot open output file: ");
  48.     std_err(*argv);
  49.     std_err("\n");
  50.     exit(2);
  51.   }
  52.  
  53.   if (iflag) signal(SIGINT, SIG_IGN);
  54.  
  55.   while ((n = read(0, buf, BLOCK_SIZE)) > 0) {
  56.     for (i = 0; i < s; i++) write(fd[i], buf, n);
  57.   }
  58.  
  59.   for (i = 0; i < s; i++)    /* Close all fd's */
  60.     close(fd[i]);
  61.   exit(0);
  62. }
  63.